var aDataSet = [];
var rQuestionnaire = [];
var lightResultatQuestionnaire;
var tickImg =  '<img id="delete" src="../images/icons/tick.png" />';
var crossImg =  '<img id="delete" src="../images/icons/cross.png" />';
var reponsesCandidat = [];
var reponsesCorrectes = [];

function getParameterByName(name){
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(function(){
	
	var data = {
		emailCandidat : getParameterByName("emailCandidat")
	};
	
	h2Candidat = $("#h2Candidat");
	h2Candidat.append("Consultation du resultat du candidat  ");
	
	$.ajax({
		type : 'Get',
		url : '/qRH-WS/services/questionnaire/consulterResultat',
		async: false,
		data: data,
		dataType : 'json',
		crossDomain : true,
		success : function(result, textStatus, jqXHR) {
			lightResultatQuestionnaire = result;
			
			h2Candidat.append(lightResultatQuestionnaire.candidat.nomCandidat+" ");
			h2Candidat.append(lightResultatQuestionnaire.candidat.prenomCandidat);
			
			rQuestionnaire = lightResultatQuestionnaire.resultatQuestionnaire;
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(xhr.responseText);
		}
	});
	
	for ( var i = 0; i < rQuestionnaire.length; i++) {
		if(rQuestionnaire[i].resultat === "true"){
			img = tickImg;
		} else {
			img = crossImg;
		}
		aDataSet.push(eval([rQuestionnaire[i].contenuQuestion, img, rQuestionnaire[i].reponsesCandidat, rQuestionnaire[i].reponsesCorrectes]));
	}
	
	$('#tableContainer').html( '<table class="display" id="dataTableResultat"></table>' );
	
	var dataTableResultat = $('#dataTableResultat').dataTable( {
		"aaData": aDataSet,
		"aoColumns": [
            { "sTitle": "Question" },
			{ "sTitle": "Resultat" },
			{ "sTitle": "Reponses candidat" },
			{ "sTitle": "Reponses correctes" }
			]
	} )
	;
	
	$('#dataTableResultat tbody tr').each(
		function() {
	        var nTds = $('td', this);
	        reponsesCandidat = eval($(nTds[2]).text().split(","));
	        reponsesCorrectes = eval($(nTds[3]).text().split(","));
	        
//	        $('td', this).slice(-3,-1).mouseover(
//        		function() {
//        			$(this).tooltip({ effect: 'slide'});
//		        }
//	        );
	    }
	);
	
	dataTableResultat.fnSetColumnVis( 2, false );
	dataTableResultat.fnSetColumnVis( 3, false );
	
	
});